#!/bin/bash
# @Author  : Bright (brt2@qq.com)
# @Create  : 2022-09-30
# @Update  : 0.1.4 / 2024-03-23

etc_dir="$(dirname `readlink -f $0`)"

# func of exec the link
function execLink(){
srcfile=$1
destfile=$2
    # 判断目标文件已存在
	if [ -L ${destfile} ] ; then
		rm ${destfile}
		echo "[+] Update the existent link file: ${destfile}"
	elif [ -f ${destfile} ] ; then
		mv ${destfile} ${destfile}.bak
		echo "[+] Backup the existent rc-file: ${destfile}"
	fi
    # 执行link操作
	ln -s ${srcfile} ${destfile}
    echo "[+] Link '${srcfile}' to '${destfile}'"
}

# func of exec the copy
function execCopy(){
srcfile=$1
destfile=$2
    # 判断目标文件已存在
	if [ -L ${destfile} ] ; then
        rm ${destfile}
    elif [ -d ${destfile} ] ; then
        read -p '>>> Exist dir '"${destfile}"', REPLACE it? (y/[N]): ' ok
        if [ ${ok}_ == "y_" ]; then
            # 备份目录
            mv ${destfile} ${destfile}.bak
            if [ $? -ne 0 ] ; then
                echo "[-] Fail to backup dir: $destfile"
                return
            else
                echo "[+] Backup the existent dir: ${destfile}"
            fi
        else
            return
        fi
	elif [ -f ${destfile} ] ; then
        read -p '>>> Exist file '"${destfile}"', REPLACE it? (y/[N]): ' ok
        if [ ${ok}_ == "y_" ]; then
            mv $destfile ${destfile}.bak
            if [ $? -ne 0 ] ; then
                echo "[-] Fail to backup file: $destfile"
                return
            else
                echo "[+] Backup the existent dir: ${destfile}"
            fi
        else
            return
        fi
    fi
    # 执行复制操作
    test -d $srcfile && cp -r $srcfile $destfile || cp $srcfile $destfile
	if [ $? -ne 0 ] ; then
		echo "[-] Fail to set the rc-file: $srcfile"
    else
        echo "[+] Copy '${srcfile}' to '${destfile}'"
	fi
}

#####################################################################

execLink $etc_dir/bashrc/.profile			$HOME/.profile
execLink $etc_dir/bashrc/.bash_aliases		$HOME/.bash_aliases
execLink $etc_dir/bashrc/.bashrc-marscode	$HOME/.bashrc
# execLink $etc_dir/bashrc/.condarc			$HOME/.condarc

execLink $etc_dir/config/git/.gitconfig		$HOME/.gitconfig
execLink $etc_dir/config/tmux.conf			$HOME/.tmux.conf
# execLink $etc_dir/config/pip				$HOME/.pip
# execLink $etc_dir/config/jupyter			$HOME/.jupyter
